# WebRTC RabbitMQ API 测试脚本
# 用于验证设备注册、信令传输等功能

$baseUrl = "http://localhost:5000"

Write-Host "=== WebRTC RabbitMQ API 测试 ===" -ForegroundColor Green

# 1. 测试设备注册
Write-Host "`n1. 测试设备注册..." -ForegroundColor Yellow

$registerBody = @{
    deviceType = "voice_phone"
    audioCodecs = @("PCMU", "PCMA", "G722")
    maxConcurrentCalls = 1
    bandwidthLimit = 64
    supportsDTMF = $true
    firmwareVersion = "1.0.0"
} | ConvertTo-Json -Depth 3

try {
    $registerResponse = Invoke-RestMethod -Uri "$baseUrl/api/webrtc-devices/test_voice_001/register" -Method POST -ContentType "application/json" -Body $registerBody
    Write-Host "✓ 设备注册成功: $($registerResponse | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "✗ 设备注册失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 2. 测试获取设备状态
Write-Host "`n2. 测试获取设备状态..." -ForegroundColor Yellow

try {
    $statusResponse = Invoke-RestMethod -Uri "$baseUrl/api/webrtc-devices/service/status" -Method GET
    Write-Host "✓ 获取服务状态成功: $($statusResponse | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "✗ 获取服务状态失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 3. 注册第二个设备
Write-Host "`n3. 注册第二个设备..." -ForegroundColor Yellow

$register2Body = @{
    deviceType = "voice_phone"
    audioCodecs = @("PCMU", "PCMA")
    maxConcurrentCalls = 1
    bandwidthLimit = 64
    supportsDTMF = $true
    firmwareVersion = "1.0.0"
} | ConvertTo-Json -Depth 3

try {
    $register2Response = Invoke-RestMethod -Uri "$baseUrl/api/webrtc-devices/test_voice_002/register" -Method POST -ContentType "application/json" -Body $register2Body
    Write-Host "✓ 第二个设备注册成功: $($register2Response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "✗ 第二个设备注册失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 4. 跳过信令消息测试（需要RabbitMQ连接）
Write-Host "`n4. 跳过信令消息测试（需要RabbitMQ连接）..." -ForegroundColor Yellow

# 5. 测试获取房间内设备列表
Write-Host "`n5. 测试获取房间内设备列表..." -ForegroundColor Yellow

try {
    $roomDevicesResponse = Invoke-RestMethod -Uri "$baseUrl/api/webrtc-devices/online" -Method GET
    Write-Host "✓ 获取在线设备列表成功: $($roomDevicesResponse | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "✗ 获取在线设备列表失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 6. 测试健康检查
Write-Host "`n6. 测试健康检查..." -ForegroundColor Yellow

try {
    $healthResponse = Invoke-RestMethod -Uri "$baseUrl/health" -Method GET
    Write-Host "✓ 健康检查成功: $($healthResponse | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "✗ 健康检查失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 7. 跳过设备心跳测试（需要RabbitMQ连接）
Write-Host "`n7. 跳过设备心跳测试（需要RabbitMQ连接）..." -ForegroundColor Yellow

Write-Host "`n=== 测试完成 ===" -ForegroundColor Green
Write-Host "请检查日志文件 logs/dev/ 目录下的详细日志信息" -ForegroundColor Cyan 